package com.ss.android.download;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

public final class DownloadInfo {
    final Context C;
    final t D;
    private final r H;
    private final g I;
    public int A;
    public long a;
    public String b;
    public boolean c;
    public String d;
    public String e;
    public String f;
    public int g;
    public int h;
    public int i;
    public int j;
    public int k;
    public int l;
    public long m;
    public String n;
    public String o;
    public String p;
    public String q;
    public long r;
    public long s;
    public String t;
    public int u;
    public boolean v;
    public String w;
    public int x;
    public boolean y;
    public String z;
    List B;
    private int E;
    private Future F;
    private n G;

    private DownloadInfo(Context arg3, t arg4, r arg5, g arg6) {
        this.B = new ArrayList();
        this.C = arg3;
        this.C.getSystemService(Context.NOTIFICATION_SERVICE);
        this.D = arg4;
        this.H = arg5;
        this.I = arg6;
        this.E = com.ss.android.download.o.a.nextInt(1001);
    }

    DownloadInfo(Context arg1, t arg2, r arg3, g arg4, byte arg5) {
        this(arg1, arg2, arg3, arg4);
    }

    private static int a(int arg1) {
        int v0;
        switch (arg1) {
            case 0: {
                v0 = 1;
                break;
            }
            case 1: {
                v0 = 2;
                break;
            }
            default: {
                v0 = 0;
                break;
            }
        }

        return v0;
    }

    public static int a(k arg7, long arg8) {
        int v0_1;
        Cursor v1 = arg7.a(ContentUris.withAppendedId(com.ss.android.download.a.a.a, arg8), new String[]{"status"}, null, null, null);
        try {
            if (!v1.moveToFirst()) {
                return 190;
            }
            v0_1 = v1.getInt(0);
            return v0_1;
        } catch (Throwable v0) {
            return 190;
        } finally {
            if (v1 != null) {
                try {
                    v1.close();
                } catch (Exception v1_1) {
                }
            }
        }
    }

    public final long a(long arg6) {
        if (this.k != 0) {
            arg6 = this.l > 0 ? this.m + (((long) this.l)) : this.m + (((long) ((this.E + 1000) * 30 * (1 << this.k - 1))));
        }

        return arg6;
    }

    public final NetworkState a() {
        NetworkState v0_1;
        NetworkInfo v0 = this.D.b();
        if (v0 == null || !v0.isConnected()) {
            v0_1 = NetworkState.NO_CONNECTION;
        } else {
            if ((this.D.c()) && !this.y) {
                v0_1 = NetworkState.CANNOT_USE_ROAMING;
                return v0_1;
            }

            int v2 = v0.getType();
            int v3 = DownloadInfo.a(v2);
            int v0_2 = this.x == -1 ? 1 : 0;
            if (v0_2 == 0 && (this.x & v3) == 0) {
                return NetworkState.TYPE_DISALLOWED_BY_REQUESTOR;
            }

            if (this.r > 0 && v2 != 1) {
                Long v0_3 = this.D.d();
                if (v0_3 != null && this.r > v0_3.longValue()) {
                    return NetworkState.UNUSABLE_DUE_TO_SIZE;
                }

                if (this.A != 0) {
                    return NetworkState.OK;
                }

                v0_3 = this.D.e();
                if (v0_3 == null) {
                    return NetworkState.OK;
                }

                if (this.r <= v0_3.longValue()) {
                    return NetworkState.OK;
                }

                return NetworkState.RECOMMENDED_UNUSABLE_DUE_TO_SIZE;
            }

            label_51:
            v0_1 = NetworkState.OK;
        }

        return v0_1;
    }

    final void a(boolean arg4) {
        Intent v0 = new Intent("android.intent.action.VIEW");
        v0.setData(this.b());
        v0.setClassName(this.C.getPackageName(), SizeLimitActivity.class.getName());
        v0.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        v0.putExtra("isWifiRequired", arg4);
        this.C.startActivity(v0);
    }

    public final boolean a(l arg8) {
        MediaScannerConnection v1;
        boolean v0 = true;
        synchronized (this) {
            if (this.u == 0) {
                if (this.g != 0 && this.g != 1) {
                    v0 = false;
                } else if (!com.ss.android.download.a.a.a(this.j)) {
                    v0 = false;
                } else {
                }
            } else {
                v0 = false;
            }

            if (v0) {
                if (com.ss.android.download.aa.c) {
                    Log.v("SsDownloadManager", "requestScan() for " + this.e);
                }
                v1 = arg8.a;
                synchronized (v1) {
                    com.ss.android.download.l.l$a v2 = new com.ss.android.download.l.l$a(this.a, this.e, this.f);
                    arg8.b.put(v2.b, v2);
                    if (arg8.a.isConnected()) {
                        v2.a(arg8.a);
                    } else {
                        arg8.a.connect();
                    }
                    return v0;
                }
            } else {
                return v0;
            }
        }
    }

    public final synchronized boolean a(ExecutorService arg9) {
        boolean v6;
        int v7 = 192;
        int v0 = 1;
        if (this.i != 1) {
            switch (this.j) {
                case 0:
                case 190:
                case 192: {
                    v6 = true;
                    break;
                }
                case 194: {
                    long v2 = this.D.a();
                    if (!this.c() && this.a(v2) <= v2) {
                        v6 = true;
                    } else {
                        v6 = false;
                    }
                    break;
                }
                case 195:
                case 196: {
                    if (!this.c() && this.a() == NetworkState.OK) {
                        v6 = true;
                    } else {
                        v6 = false;
                    }
                    break;
                }
                case 198: {
                    v6 = false;
                    break;
                }
                case 199: {
                    v6 = Environment.getExternalStorageState().equals("mounted");
                    break;
                }
                default:
                    v6 = false;
                    break;
            }
        } else {
            label_8:
            v6 = false;
        }

        label_9:
        if (this.F == null || (this.F.isDone())) {
            v0 = 0;
        }

        if ((v6) && v0 == 0) {
            if (this.j != v7) {
                this.j = 192;
                ContentValues v0_2 = new ContentValues();
                v0_2.put("status", Integer.valueOf(this.j));
                com.ss.android.download.k.a(this.C).a(this.b(), v0_2, null, null);
            }

            this.G = new n(this.C, this.D, this, this.H, this.I);
            this.F = arg9.submit(this.G);
        }
        return v6;
    }

    public final Uri b() {
        return ContentUris.withAppendedId(com.ss.android.download.a.a.a, this.a);
    }

    private boolean c() {
        boolean v0 = true;
        NetworkInfo v1 = this.D.b();
        if (v1 == null || !v1.isConnected() || DownloadInfo.a(v1.getType()) != 1) {
            v0 = false;
        }

        return v0;
    }

    public enum NetworkState {
        OK,
        NO_CONNECTION,
        UNUSABLE_DUE_TO_SIZE,
        RECOMMENDED_UNUSABLE_DUE_TO_SIZE,
        CANNOT_USE_ROAMING,
        TYPE_DISALLOWED_BY_REQUESTOR
    }

    public static final class DownloadInfo$a {
        private k a;
        private Cursor b;

        public DownloadInfo$a(k arg1, Cursor arg2) {
            this.a = arg1;
            this.b = arg2;
        }

        private static void a(DownloadInfo arg2, String arg3, String arg4) {
            arg2.B.add(Pair.create(arg3, arg4));
        }

        private String a(String arg3) {
            String v0 = this.b.getString(this.b.getColumnIndexOrThrow(arg3));
            if (TextUtils.isEmpty(v0)) {
                v0 = null;
            }

            return v0;
        }

        public final DownloadInfo a(Context arg9, t arg10, r arg11, g arg12) {
            DownloadInfo v0 = new DownloadInfo(arg9, arg10, arg11, arg12, (byte) 0);
            this.a(v0);
            v0.B.clear();
            Cursor v1 = this.a.a(Uri.withAppendedPath(v0.b(), "headers"), null, null, null, null);
            int v2_1 = v1.getColumnIndexOrThrow("header");
            int v3 = v1.getColumnIndexOrThrow("value");
            v1.moveToFirst();
            while (!v1.isAfterLast()) {
                com.ss.android.download.DownloadInfo.DownloadInfo$a.a(v0, v1.getString(v2_1), v1.getString(v3));
                v1.moveToNext();
            }
            if (v1 != null) {
                try {
                    v1.close();
                } catch (Exception v1_1) {
                }
            }

            if (v0.o != null) {
                DownloadInfo$a.a(v0, "Cookie", v0.o);
            }

            if (v0.q != null) {
                DownloadInfo$a.a(v0, "Referer", v0.q);
            }
            return v0;
        }

        public final void a(DownloadInfo arg7) {
            boolean v1 = true;
            arg7.a = this.c("_id").longValue();
            arg7.b = this.a("uri");
            boolean v0 = this.b("no_integrity").intValue() == 1 ? true : false;
            arg7.c = v0;
            arg7.d = this.a("hint");
            arg7.e = this.a("_data");
            arg7.f = this.a("mimetype");
            arg7.g = this.b("destination").intValue();
            arg7.h = this.b("visibility").intValue();
            arg7.j = this.b("status").intValue();
            arg7.k = this.b("numfailed").intValue();
            arg7.l = this.b("method").intValue() & 268435455;
            arg7.m = this.c("lastmod").longValue();
            arg7.n = this.a("notificationpackage");
            this.a("notificationextras");
            arg7.o = this.a("cookiedata");
            arg7.p = this.a("useragent");
            arg7.q = this.a("referer");
            arg7.r = this.c("total_bytes").longValue();
            arg7.s = this.c("current_bytes").longValue();
            arg7.t = this.a("etag");
            arg7.u = this.b("scanned").intValue();
            v0 = this.b("deleted").intValue() == 1 ? true : false;
            arg7.v = v0;
            arg7.w = this.a("mediaprovider_uri");
            arg7.x = this.b("allowed_network_types").intValue();
            if (this.b("allow_roaming").intValue() == 0) {
                v1 = false;
            }

            arg7.y = v1;
            arg7.z = this.a("title");
            this.a("description");
            arg7.A = this.b("bypass_recommended_size_limit").intValue();
            synchronized (this) {
                arg7.i = this.b("control").intValue();
            }
        }

        private Integer b(String arg3) {
            return Integer.valueOf(this.b.getInt(this.b.getColumnIndexOrThrow(arg3)));
        }

        private Long c(String arg3) {
            return Long.valueOf(this.b.getLong(this.b.getColumnIndexOrThrow(arg3)));
        }
    }
}

